#include "RenderSystem.h"
#include "GraphicComponents.h"
#include "PositionComponent.h"
#include "Base/Exceptions.h"
#include "Base/Logger.h"
#include "Base/BPL.h"
#include <boost/lexical_cast.hpp>
#include <string>

RenderSystem::RenderSystem()
{
	
}
void RenderSystem::update(float dt)
{
    Assert(mpt_app != NULL);
	
	mpt_app->clear(sf::Color(30,140,150));

	sf::Vector2f posCenter = mpt_app->getView().getCenter();
	int sizeTile = entityManager->sizeTile;

	// METTRE UN RAYON DE NEIGHBOURHOOD
	std::deque<int> dynamicEntities = entityManager->getGrid()->getNeighbourhoodAt(0,posCenter);

	// POUR LES LAYERS : DRAW DANS DES RENDERIMAGES POUR CHAQUE LAYER !!!!

    for(std::deque<int>::iterator itr = dynamicEntities.begin(); itr!=dynamicEntities.end(); )
    {
		if(entityManager->hasEntity((*itr)))
		{
			if(mpt_entitiesID.find(*itr)!=mpt_entitiesID.end())
			{
				GraphicComponentPtr graphEnt = entityManager->getEntity(*itr)->getAs<GraphicComponent>();
				if(graphEnt->imageType == GraphicComponent::NORMAL)
				{
					// FAIRE AVEC LE TRASFORMABLE
					graphEnt->m_transform.setPosition(entityManager->getEntity(*itr)->getAs<PositionComponent>()->m_position);
					mpt_app->draw(*graphEnt->m_sprite, graphEnt->m_transform.getTransform());
				}
				else
				{
					//UPDATE DU MODEL (meme si statique)
					ModelGraphicComponentPtr modelPtr = boost::dynamic_pointer_cast<ModelGraphicComponent>(graphEnt);
					modelPtr->model.update();
					modelPtr->model.setSpritePosition(entityManager->getEntity(*itr)->getAs<PositionComponent>()->m_position);
					
					mpt_app->draw(modelPtr->model.getCurrentSprite());
				}
			}
			itr++;
		}
		else
		{
			std::string str = "BUG RENDER : ";
			str += boost::lexical_cast<std::string>(*itr);
			BPL::Logger::log(str.c_str());
			itr = dynamicEntities.erase(itr);
		}
    }

	sf::Vector2i posCenteri(posCenter.x/sizeTile, posCenter.y/sizeTile);
	std::vector<int> staticEntities = entityManager->getMap()->getNeighbourhoodAt(posCenteri,15,15);
	for(std::vector<int>::iterator itr = staticEntities.begin(); itr!=staticEntities.end(); )
    {
		if(entityManager->hasEntity((*itr)))
		{
			if(mpt_entitiesID.find(*itr)!=mpt_entitiesID.end())
			{
				GraphicComponentPtr graphEnt = entityManager->getEntity(*itr)->getAs<GraphicComponent>();
				if(graphEnt->imageType == GraphicComponent::NORMAL)
				{
					// TRANSFORMABLE
					graphEnt->m_transform.setPosition(entityManager->getEntity(*itr)->getAs<PositionComponent>()->m_position);
					mpt_app->draw(*graphEnt->m_sprite, graphEnt->m_transform.getTransform());
				}
				else
				{
					//UPDATE DU MODEL (meme si statique)
					ModelGraphicComponentPtr modelPtr = boost::dynamic_pointer_cast<ModelGraphicComponent>(graphEnt);
					modelPtr->model.update();
					modelPtr->model.setSpritePosition(entityManager->getEntity(*itr)->getAs<PositionComponent>()->m_position);
					
					mpt_app->draw(modelPtr->model.getCurrentSprite());
				}
			}
			itr++;
		}
		else
		{
			itr = staticEntities.erase(itr);
		}
    }

    mpt_app->display();
}

void RenderSystem::addEntity(int ent)
{
	if(!entityManager->hasEntity(ent))
		return;

	
    mpt_entitiesID.insert(ent);
	entityManager->getEntity(ent)->addSystem(this);
}
